<script setup lang="ts">
import { postLoginPasswordAPI } from '@/apis/comcom'
import { useUserStore } from '@/stores/user'
import { ref } from 'vue'

defineOptions({
  options: {
    styleIsolation: 'shared',
  },
})
const formData = ref({
  mobile: '13230000035',
  password: 'abc12345',
})
const formRules = ref({
  password: {
    rules: [
      { required: true, errorMessage: '请输入密码' },
      { pattern: 'abc12345', errorMessage: '请输入正确的密码' },
    ],
  },
  mobile: {
    rules: [
      { required: true, errorMessage: '请输入手机号' },
      {
        patttern:
          /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/,
        errorMessage: '请输入正确的手机号',
      },
    ],
  },
})
const isAgree = ref(false)
const form = ref<UniHelper.UniFormsInstance>()
const handleLogin = async () => {
  try {
    await form.value?.validate?.()
    if (!isAgree.value) return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })
    const res = await postLoginPasswordAPI(formData.value)
    console.log(res)
    const userStore = useUserStore()
    userStore.setToken(res.data.token)
    userStore.setUserInfo(res.data)
    uni.showToast({ title: '登录成功', icon: 'none' })
    uni.switchTab({ url: '/pages/index/index' })
  } catch {}
}
</script>

<template>
  <uni-forms class="login-form" ref="form" :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="formData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="password">
      <uni-easyinput
        type="password"
        placeholder="请输入密码"
        :input-border="false"
        placeholder-style="color: #C3C3C5"
        v-model="formData.password"
      />
    </uni-forms-item>
    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="handleLogin">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" "> 忘记密码？ </navigator>
  </uni-forms>
</template>

<style lang="scss">
@import './styles.scss';
</style>
